//
//  YWSelectAtUserTableViewController.m
//  maintain
//
//  Created by kyo on 16/11/21.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWSelectAtUserTableViewController.h"
#import "UIColor+CustomColors.h"
#import "UIScrollView+EmptyDataSet.h"

#import "YWHandler.h"
#import "YWSelectContanctCell.h"

@interface YWSelectAtUserTableViewController ()<DZNEmptyDataSetDelegate, DZNEmptyDataSetSource>{
    NSIndexPath *lastIndexPath;
}
@property (nonatomic, strong) NSMutableArray *dateSource;
//选择的人的数组
@property (nonatomic, strong) NSMutableArray *selectedContactArray;
@end

@implementation YWSelectAtUserTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = [NSString stringWithFormat:@"%@@人",NSLocalizedString(@"select", nil)];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self initTableView];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"finish", nil) style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonItemEvent:)];
}

- (void)initTableView{
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.separatorColor = [UIColor customLineColor];
    self.tableView.rowHeight = 60.0f;
    self.tableView.tableFooterView = [[UIView alloc]init];
    self.tableView.emptyDataSetDelegate = self;
    self.tableView.emptyDataSetSource = self;
    [self.tableView setEditing:YES];
}

- (NSMutableArray *)selectedContactArray{
    if (!_selectedContactArray) {
        _selectedContactArray = [NSMutableArray array];
    }
    return _selectedContactArray;
}

- (void)setAllUserArray:(NSArray *)allUserArray{
    _allUserArray = allUserArray;
    for (YWHandler *handler in allUserArray) {
        NSInteger uniqueId = handler.uniqueId;
        NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:@"%K = %ld", @"uniqueId", uniqueId];
        NSArray *array = [[self.dateSource filteredArrayUsingPredicate:searchPredicate] mutableCopy];
        if (array.count==0) {
            [self.dateSource addObject:handler];
        }
    }
}

- (NSMutableArray *)dateSource{
    if (!_dateSource) {
        _dateSource = [NSMutableArray array];
    }
    return _dateSource;
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [_dateSource count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YWSelectContanctCell *cell = [YWSelectContanctCell cellWithTableView:tableView];
    YWHandler *contact = _dateSource[indexPath.row];
    cell.contact = contact;
    return cell;
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    YWHandler *contact = _dateSource[indexPath.row];
    if ([self.selectedContactArray containsObject:contact]) {
        [tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
    }else{
        [tableView deselectRowAtIndexPath:indexPath animated:NO];
    }
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    YWHandler *contact = _dateSource[indexPath.row];
    
    //单选
    if (![self.selectedContactArray containsObject:contact]) {
        [self.selectedContactArray removeAllObjects];
        [self.selectedContactArray addObject:contact];
    }
    if (lastIndexPath) {
        [tableView deselectRowAtIndexPath:lastIndexPath animated:NO];
    }
    lastIndexPath = indexPath;
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    YWHandler *contact = _dateSource[indexPath.row];
    if ([self.selectedContactArray containsObject:contact]) {
        [self.selectedContactArray removeObject:contact];
    }
    lastIndexPath = nil;
}

#pragma mark - private method
- (void)rightBarButtonItemEvent:(id)sender{
    //单选
    if ([self.delegate respondsToSelector:@selector(selectAtUser:)]) {
        if (_selectedContactArray.count==1) {
            [self.delegate selectAtUser:_selectedContactArray[0]];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [self.delegate selectAtUser:nil];
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
